package com.z.pms1.service.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.z.pms1.dao.LoginDao;
import com.z.pms1.model.C_user;
import com.z.pms1.service.LoginService;

@Service("loginService")
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginDao loginDao;
	
	public boolean login(String user_id, String passwd) {
		if (null == user_id || null == passwd) {
			return false;
		}
		C_user c_user = loginDao.find(user_id);
		if (null == c_user) {
			return false;
		}
		try {
			/**
			 * encode password
			 */
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] bytes = md.digest(passwd.getBytes());
			StringBuffer strBuf = new StringBuffer();
			for (Byte b : bytes) {
				strBuf.append(b.toString());
			}
			if (strBuf.toString().equals(c_user.getPasswd())) {
				return true;
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return false;
	}

}
